package com.wlps.service.impl;

import com.wlps.entity.Order;
import com.wlps.entity.OrderRecord;
import com.wlps.mapper.OrderRecordMapper;
import com.wlps.service.IOrderRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlps.service.IOrderService;
import com.wlps.vo.OperatorNumberVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单操作记录表(order_record)（订单状态表）用户查看订单每个时段的状态 服务实现类
 * </p>
 *
 * @author ZWYZY
 * @since 2020-07-31
 */
@Service
@Slf4j
public class OrderRecordServiceImpl extends ServiceImpl<OrderRecordMapper, OrderRecord> implements IOrderRecordService {
    @Resource
    private IOrderService orderService;
    @Override
    public Map<String, OperatorNumberVo> queryAllOperator() {
        List<OrderRecord> list = this.list();
        Map<String, OperatorNumberVo> map = new HashMap<>();
        for(OrderRecord data:list){
            OperatorNumberVo operatorNumberVo;
            if(!map.containsKey(data.getOperator())) {
                operatorNumberVo = new OperatorNumberVo();
                Long orderId = data.getOrderId();
                Order byId = orderService.getById(orderId);
                if(byId==null){
                    log.error("不存在ID为"+orderId+"的订单");
                    throw new RuntimeException("订单:"+orderId+"没有记录orderRecord表");
                }
                operatorNumberVo.setMoney(byId.getTheTotalAmountOfOrders());
                operatorNumberVo.setName(data.getOperator());
            }
            else operatorNumberVo = map.get(data.getOperator());
            Integer orderType = data.getOrderType();
            if(orderType==0)
                operatorNumberVo.setNewOrder(operatorNumberVo.getNewOrder()+1);
            else if (orderType==1)
                operatorNumberVo.setExitOrder(operatorNumberVo.getExitOrder()+1);
            else if (orderType==2)
                operatorNumberVo.setChangeOrder(operatorNumberVo.getChangeOrder()+1);
            else if(orderType==3)
                operatorNumberVo.setExitCommodity(operatorNumberVo.getExitOrder()+1);
            map.put(data.getOperator(),operatorNumberVo);
        }

        return map;
    }
}
